package com.sanyuan.cloud.module.system.dal.mysql.nongmo;

import com.sanyuan.cloud.framework.mybatis.core.mapper.BaseMapperX;
import com.sanyuan.cloud.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.sanyuan.cloud.module.system.controller.app.position.req.VisitReqVO;
import com.sanyuan.cloud.module.system.dal.dataobject.nongmo.SystemPositionUserDO;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author szw
 * @description 针对表【system_position_user(标点位置用户关联表)】的数据库操作Mapper
 * @createDate 2025-03-20 15:30:53
 * @Entity generator.domain.SystemPositionUser
 */
@Mapper
public interface SystemPositionUserMapper extends BaseMapperX<SystemPositionUserDO> {

    default SystemPositionUserDO selectPositionUserByPositionId(String positionId){
        return selectOne(new LambdaQueryWrapperX<SystemPositionUserDO>()
                .eq(SystemPositionUserDO::getPositionId, positionId)
                .last("limit 1")
        );
    }

    default List<SystemPositionUserDO> selectUserAndTimeSlot(VisitReqVO reqVO, LocalDateTime beginDate, LocalDateTime endDate){
        return selectList(new LambdaQueryWrapperX<SystemPositionUserDO>()
                .eq(SystemPositionUserDO::getUserId, reqVO.getUserId())
                .geIfPresent(SystemPositionUserDO::getCreateTime, beginDate)
                .leIfPresent(SystemPositionUserDO::getCreateTime, endDate)
        );
    }

    default List<SystemPositionUserDO> selectByPositionIdAndUserId(List<Long> positionId, Long userId,LocalDateTime startDate,LocalDateTime endDate){
        return selectList(new LambdaQueryWrapperX<SystemPositionUserDO>()
                .eqIfPresent(SystemPositionUserDO::getUserId, userId)
                .geIfPresent(SystemPositionUserDO::getCreateTime, startDate)
                .leIfPresent(SystemPositionUserDO::getCreateTime, endDate)
                .in(SystemPositionUserDO::getPositionId, positionId)
                .orderByDesc(SystemPositionUserDO::getUpdateTime)
        );
    }

    default List<SystemPositionUserDO> selectPositionUserByPositionIds(List<Long> positionIds){
        return selectList(new LambdaQueryWrapperX<SystemPositionUserDO>()
                .in(SystemPositionUserDO::getPositionId, positionIds)
                .orderByDesc(SystemPositionUserDO::getUpdateTime)
        );
    }

    default SystemPositionUserDO selectPositionUserByPositionId(Long positionId){
        return selectOne(new LambdaQueryWrapperX<SystemPositionUserDO>()
                .eq(SystemPositionUserDO::getPositionId, positionId)
                .orderByDesc(SystemPositionUserDO::getUpdateTime)
                .last("limit 1")
        );
    }

    default SystemPositionUserDO selectPositionUserByPositionId(Long positionId, Long userId){
        return selectOne(new LambdaQueryWrapperX<SystemPositionUserDO>()
                .eq(SystemPositionUserDO::getPositionId, positionId)
                .eq(SystemPositionUserDO::getUserId, userId)
                .orderByDesc(SystemPositionUserDO::getUpdateTime)
                .last("limit 1")
        );
    }

    default int deleteByPositionId(Long positionId){
        return delete(new LambdaQueryWrapperX<SystemPositionUserDO>()
                .eq(SystemPositionUserDO::getPositionId, positionId));
    }

    default List<SystemPositionUserDO> selectPositionUserByUserId(Long loginUserId,String startDate, String endDate){
        return selectList(new LambdaQueryWrapperX<SystemPositionUserDO>()
                .eq(SystemPositionUserDO::getUserId, loginUserId)
                .geIfPresent(SystemPositionUserDO::getCreateTime, startDate)
                .leIfPresent(SystemPositionUserDO::getCreateTime, endDate)
                .orderByDesc(SystemPositionUserDO::getCreateTime)
        );
    }

    default SystemPositionUserDO selectOneUserAndTimeSlot(VisitReqVO reqVO, LocalDateTime beginDate, LocalDateTime endDate){
        return selectOne(new LambdaQueryWrapperX<SystemPositionUserDO>()
                .eq(SystemPositionUserDO::getUserId, reqVO.getUserId())
                .geIfPresent(SystemPositionUserDO::getCreateTime, beginDate)
                .leIfPresent(SystemPositionUserDO::getCreateTime, endDate)
                .orderByDesc(SystemPositionUserDO::getCreateTime)
                .last("limit 1")
        );
    }
}




